<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Model;

use Think\Model;

class UserModel extends Model {
   
        protected $_validate=array(
                  array("username","require","请填写账号",1,"regex",1),
                  array("username","","账号已经存在",0,"unique",1),
                  array("password","require","请输入密码",2,"regex",2),//修改时验证
                  array("password","require","请输入密码",1,"regex",1),//新增时验证
                  array("repassword","password","密码与确认密码不一致",0,"confirm"),
                  array("email","email","邮箱格式不正确",1,"regex"),
                  array("id,email","_ckEmail","邮箱已经存在",1,"callback",3),
                  array("phone","require","手机号码格式不正确",1,"regex"),
                  array("money","currency","钻石格式不正确",1,"regex"),
                );

        //验证邮箱
        protected function _ckEmail($data){
                
                $res=$this->where("email=%d",$data["email"])->find();                
                //新增或修改（邮箱不存在）
                if(!$res){
                    return true;
                //修改（没有修改邮箱）
                }else if($res["id"]==$data["id"]){
                    return true;
                }else{
                    return false;
                }
        }

        //用户新增/修改
        public function editData($data){
            
            if(!$data){            
              $this->error="数据不能为空";
              return false;
            }

            if(!($this->create($data))){
                $this->error=$this->getError();
                return false;
            }

            //新增
            if(empty($data["id"])){

                $this->password = md5($data["password"]);
                $this->reg_time = date("Y-m-d H:i:s");

                $res=$this->add();
                if(!$res){
                    $this->error="添加失败，请重试";
                    return false;
                }
            }else{

                //如果有输入新密码
                if($data["password"] && $data["repassword"]){
                    $this->password=md5($data["password"]);
                }else{
                    unset($this->password);
                }

                $res=$this->save();
                if($res===false){
                    $this->error="修改失败，请重试";
                    return false;
                }
            }
            return true;
        }

        //根据id查询用户信息
        public function getUserById($id){

            $res=$this->where("id=%s",$id)->find();
            
            return $res;
        }

        //根据年份查询每月用户个数
        public function getUserByDate($year){


            $str_year="";
            for($month=1; $month<=12; $month++){
                if($month<10) $month="0".$month;
                $sql="select count(*) as number from cloud_user where left(reg_time,7)='{$year}-{$month}'";                
                $count=$this->query($sql);
                $str_year.=$count[0]["number"].",";
            }

           /* $sql="SELECT LEFT( reg_time, 7 ) AS reg_date, COUNT( reg_time ) AS count FROM cloud_user WHERE LEFT( reg_time, 4 ) =  '{$year}' GROUP BY LEFT( reg_time, 7 )";        
            $arr_year=$this->query($sql);*/

            return  substr($str_year,0,-1);
        }

        //删除用户信息
        public function delData($data){

            $res=$this->where(array("id"=>array("in",$data)))->delete();
            return $res;
        }
    
   }


?>
